//
//  LoginViewController.m
//  UI16.真.豆瓣
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 Amydom. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegisterViewController.h"
#import "OwnerViewController.h"
#import "MBProgressHUD.h"
#import "SingLeton.h"
#import "RegisterView.h"
#import "Movie.h"
#import "UserModel.h"
@interface LoginViewController ()<MBProgressHUDDelegate>

@property (nonatomic , retain)LoginView *login;
@property (nonatomic , retain)RegisterViewController *regist;
@property (nonatomic, retain)MBProgressHUD *hud;
@property (nonatomic , retain)UIAlertController *alert;
@property (nonatomic ,copy)NSString *name;
@property (nonatomic ,copy)NSString *passW;

@property (nonatomic ,assign)BOOL isSuccess;

@end

@implementation LoginViewController
-(void)dealloc{
    [_name release];
    [_passW release];
    [_hud release];
    [_alert release];
    [_login release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户登陆";
    self.login = [[LoginView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:self.login];
    [_login release];
    
    [self.login.ButtonZC addTarget:self action:@selector(ButtonZCAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.login.ButtonDL addTarget:self action:@selector(ButtonDLAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.regist = [[RegisterViewController alloc]init];
    [[SingLeton shareSingleton]selectUser];
}



- (void)ButtonZCAction:(UIButton *)btn{
    
    [self.navigationController pushViewController:self.regist animated:YES];
    
    
}


- (void)ButtonDLAction:(UIButton *)btn{
    
    
    NSMutableArray *array = [SingLeton shareSingleton].arrayUser;
    UserModel *userStr= [[UserModel alloc]init];
    
//    if ([array containsObject:self.login.UserField.text]) {
//        
//        NSLog(@"用户已经存在");
//        
//    }
    

    for (int i = 0; i < array.count; i++) {
        
        userStr = [array objectAtIndex:i];
       self.name = userStr.UserName;
        self.passW = userStr.passWord;
        
         if (self.login.UserField.text == self.name && self.login.MField.text == self.passW) {
             
             self.isSuccess = YES;
             [[NSUserDefaults standardUserDefaults] setObject:self.login.UserField.text forKey:@"name"];
             break;
         }else{
             self.isSuccess = NO;
             NSLog(@"0");
         }
        
    }
   

    if (self.isSuccess == YES) {
        
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isLogin"];
        
        self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.hud.labelText = @"登陆成功";
        self.hud.mode = MBProgressHUDModeText;
        self.hud.delegate = self;
        [self.hud hide:YES afterDelay:0.5];
        
     
        
        NSLog(@"登陆成功");
        
    } else {
        
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"isLogin"];
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码错误" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        // 创建提示框按钮
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        // 将按钮放在提示框上
        [alertC addAction:action];
    }
    
}


- (void)hudWasHidden:(MBProgressHUD *)hud{
    
    [self.navigationController popViewControllerAnimated:YES];
    

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
